package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author 董乃铭
 * @Date   2020/4/15 10:35
 * @Version v1.0
 * @Description
 */
@RestController
@RequestMapping("menu")
@Slf4j
public class MenuController {

    @Reference
    private MenuService menuService;



    //删除菜单
    @RequestMapping("delete")
    public Result delete(Integer id){

        try {
            menuService.delete(id);

            return new Result(true,"删除菜单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除菜单失败");
        }
    }




    //编辑更改菜单
    @RequestMapping("edit")
    public Result edit(@RequestBody Menu menu){

        try {
            menuService.edit(menu);

            return new Result(true,"更改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"更改失败");
        }


    }



    //点击编辑回显功能
    @RequestMapping("findById")
    public Result findById(Integer id){

        try {
            Menu menu = menuService.findById(id);

            return new Result(true,"回显成功",menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"回显失败");
        }


    }



    //菜单添加
    @RequestMapping("add")
    public Result add(@RequestBody Menu menu){

        try {
            menuService.add(menu);

            return new Result(true,"添加菜单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加菜单失败");
        }

    }



    //菜单分页查询
    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

        try {
            PageResult pageResult = menuService.pageQuery(queryPageBean);

            return pageResult;
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(0L,null);
        }

    }
}
